package com.ffcs.web.controllers;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.ffcs.web.model.dto.BaseResponse;
import com.ffcs.web.model.dto.CounselorListResponse;
import com.ffcs.web.model.dto.CounselorResponse;
import com.ffcs.web.model.dto.OrderPing;
import com.ffcs.web.model.dto.PingLunResponse;
import com.ffcs.web.model.dto.UserInfo;
import com.ffcs.web.model.entity.Advert;
import com.ffcs.web.model.entity.Counselor;
import com.ffcs.web.model.entity.FreeAsk;
import com.ffcs.web.model.entity.Order;
import com.ffcs.web.model.entity.PicNews;
import com.ffcs.web.model.entity.PingLun;
import com.ffcs.web.model.entity.WxUser;
import com.ffcs.web.model.requests.AddCounseRequest;
import com.ffcs.web.model.requests.CounselorListRequest;
import com.ffcs.web.services.AuthCodeService;
import com.ffcs.web.services.CollectNewsService;
import com.ffcs.web.services.CounselorService;
import com.ffcs.web.services.FreeAskService;
import com.ffcs.web.services.OrderService;
import com.ffcs.web.services.PicNewsService;
import com.ffcs.web.services.WxUserService;
import com.ffcs.web.utils.ChinaeseUtil;
import com.ffcs.web.utils.CommonConfig;
import com.ffcs.web.utils.HttpGetUtil;
import com.ffcs.web.utils.JsonUtil;

/**
 * 咨询师相关控制器
 * @author Administrator
 *
 */
@Controller
public class CounseController {

	private Logger logger = Logger.getLogger(CounseController.class);
	
	@Autowired
	private CounselorService counselorService;
	
	@Autowired
	private AuthCodeService authCodeService;
	
	@Autowired
	private PicNewsService picNewsService;
	@Autowired
	private WxUserService wxUserService;
	@Autowired
	private CollectNewsService collectNewsService;
	@Autowired
	private OrderService orderService;
	@Autowired
	private FreeAskService freeAskService;
	
	private static String PIC_ADRESS=CommonConfig.getProperty("PIC_ADRESS");
	
	/**
	 * 跳往专家库
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("goCounseKu")
	@ResponseBody
	public void goCounseKu(HttpServletRequest request,HttpServletResponse response) throws IOException{
		String type=request.getParameter("counseType");
		if(null!=type && !"".equals(type)){
			response.sendRedirect("counseKu.jsp?type_="+type+"&txtsearch=noshuju");
			return ;
		}
		String txtsearch=request.getParameter("txtsearch");
		txtsearch=ChinaeseUtil.CharTOChinaese(txtsearch);
		if(null!=txtsearch && !"".equals(txtsearch)){
//			System.out.println(txtsearch);
//			System.out.println(txtsearch);
//			System.out.println(txtsearch);
//			System.out.println(txtsearch);
//			response.sendRedirect("counseKu.jsp?txtsearch="+txtsearch+"&type_=100");
//			return ;
		}
		response.sendRedirect("counseKu.jsp");
	}
	
	/**
	 * 注册咨询师
	 * @param request
	 * @param addCounseRequest
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("updateCounselor")
	public void updateCounselor(HttpServletRequest request,HttpServletResponse response,AddCounseRequest addCounseRequest) throws IOException{
 	   	   
		   System.out.println(addCounseRequest.getCounseType());
		
		   String counseTel=(String) request.getSession().getAttribute("counseTel");
 	   	   addCounseRequest.setCounseTel(counseTel);
		   //转型为MultipartHttpRequest(重点的所在)
	       MultipartHttpServletRequest multipartRequest=null;
	      
	       try {
	            multipartRequest  =  (MultipartHttpServletRequest) request; 
	       } catch (Exception e) {
			   logger.error("咨询师头像为空");
			   e.printStackTrace();
			   return ;
	       }
	       
//	       String saveRootPath=request.getRealPath("/");//图片保存的根目录
	       
	       //  获得第1张图片
	       MultipartFile personHead  =  multipartRequest.getFile("personHead"); 
	       if("".endsWith(personHead.getOriginalFilename())){
	    	   try {
	    		   counselorService.addCounse(addCounseRequest,"0");
			   } catch (Exception e) {
				   e.printStackTrace();
				   response.sendRedirect("goToUpdateMyData");
				   return ;
			   }
	    	   request.getSession().setAttribute("counseTel",addCounseRequest.getCounseTel());
		       response.sendRedirect("business/myCounse.jsp");
	       }else{
	    	   String personHeadUrl=addImage(personHead, PIC_ADRESS, addCounseRequest.getCounseTel());
		       System.out.println("personHeadUrl="+personHeadUrl);
	    	   if(null!=personHeadUrl && !personHeadUrl.equals("")){
		    	   String s[]=personHeadUrl.split("[.]");
		    	   personHeadUrl=addCounseRequest.getCounseTel()+"."+s[s.length-1];
		    	   try {
		    		   counselorService.addCounse(addCounseRequest,personHeadUrl);
				   } catch (Exception e) {
					   e.printStackTrace();
					   response.sendRedirect("goToUpdateMyData");
					   return ;
				   }
		    	   
		       }
		       request.getSession().setAttribute("counseTel",addCounseRequest.getCounseTel());
		       response.sendRedirect("business/myCounse.jsp");
	       }
		}
	
	
	/**
	 * 注册咨询师
	 * @param request
	 * @param addCounseRequest
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("registCounselor")
	public void addCounse(HttpServletRequest request,HttpServletResponse response,AddCounseRequest addCounseRequest) throws IOException{
	   if(!authCodeService.isAuthCode(addCounseRequest.getCounseTel(),addCounseRequest.getCode())){
		   logger.error("验证码已失效或错误");
		   response.sendRedirect("business/regCounselor.jsp");
		   return ;
	   }
		
	   //转型为MultipartHttpRequest(重点的所在)
       MultipartHttpServletRequest multipartRequest=null;
      
       try {
            multipartRequest  =  (MultipartHttpServletRequest) request; 
       } catch (Exception e) {
		   logger.error("咨询师头像为空");
		   response.sendRedirect("business/regCounselor.jsp");
		   return ;
       }
       
//       String saveRootPath=YUYIN_ADRESS;//图片保存的根目录
       
       //  获得第1张图片
       MultipartFile personHead  =  multipartRequest.getFile("personHead"); 
       String personHeadUrl=addImage(personHead, PIC_ADRESS, addCounseRequest.getCounseTel());
       if(null!=personHeadUrl && !personHeadUrl.equals("")){
    	   String s[]=personHeadUrl.split("[.]");
    	   personHeadUrl=addCounseRequest.getCounseTel()+"."+s[s.length-1];
    	   try {
    		   counselorService.addCounse(addCounseRequest,personHeadUrl);
		   } catch (Exception e) {
			   response.sendRedirect("business/regCounselor.jsp");
			   return ;
		   }
    	   
       }
//       request.getSession().setAttribute("counseTel",addCounseRequest.getCounseTel());
       response.sendRedirect("business/login.jsp");
	}
	
	/**
	 * 咨询师列表
	 * @param counseTel
	 * @return
	 */
	@RequestMapping("getCounseList")
	@ResponseBody
	public CounselorListResponse getCounseList(HttpServletRequest request,CounselorListRequest counselorListRequest){
		
//		if((null==counselorListRequest.getCityName() || "".equals(counselorListRequest.getCityName())) && !"1".equals(counselorListRequest.getFlag())){
//			if(null!=request.getSession().getAttribute("cityName") && !"".equals(request.getSession().getAttribute("cityName"))){
//				String cityName=(String) request.getSession().getAttribute("cityName");
//				counselorListRequest.setCityName(cityName.substring(0,2));
//			}
//		}
		
		
		CounselorListResponse counselorListResponse=new CounselorListResponse();
		List<Counselor> counselorList=counselorService.getCounseList(counselorListRequest);
		for (Counselor counselor : counselorList) {
			if(null!=counselor.getGoodDirection()){
				String gd="";
				if(counselor.getGoodDirection().contains("1")){
					gd+="学业倦怠,";
				}
				if(counselor.getGoodDirection().contains("2")){
					gd+="亲子关系,";
				}
				if(counselor.getGoodDirection().contains("3")){
					gd+="教育理念,";
				}
				if(counselor.getGoodDirection().contains("4")){
					gd+="学习习惯,";
				}
				if(counselor.getGoodDirection().contains("5")){
					gd+="人际关系,";
				}
				if(counselor.getGoodDirection().contains("6")){
					gd+="沉迷网络,";
				}
				if(counselor.getGoodDirection().contains("7")){
					gd+="情绪管理,";
				}
				if(counselor.getGoodDirection().contains("8")){
					gd+="自控能力,";
				}
				gd=gd.substring(0, gd.length()-1);
				counselor.setGoodDirection(gd);
			}else{
				counselor.setGoodDirection("");
			}
			counselor.setPersonHead(CommonConfig.getProperty("PIC_URL_FRONT")+counselor.getPersonHead());
			if(counselor.getProfessional().contains("html")){
				String tempStr=counselor.getProfessional();
				tempStr=tempStr.replaceAll("<html>", "");
				tempStr=tempStr.replaceAll("</html>", "");
				tempStr=tempStr.replaceAll("<title>", "");
				tempStr=tempStr.replaceAll("</title>", "");
				tempStr=tempStr.replaceAll("<head>", "");
				tempStr=tempStr.replaceAll("</head>", "");
				tempStr=tempStr.replaceAll("</body>", "");
				tempStr=tempStr.replaceAll("<body>", "");
				
				String a=tempStr;
				String temps="";
				String temps2="";
				for (int i = 0; i < 5; i++) {
					if(a.contains("<br />") && a.contains("img")){
						temps+=a.split("<br />")[0]+"<br />";
						temps2=a.split("<br />")[0]+"<br />";
						a=a.replace(temps2, "");
					}
				}
				System.out.println(temps);
				counselor.setProfessional(temps);
			}
		}
		counselorListResponse.setResultCode("0");
		counselorListResponse.setResultDesc("success");
		counselorListResponse.setCounselor(counselorList);
		return counselorListResponse;
	}
	
	/**
	 * 获取咨询师资料
	 * @param request
	 * @param counseTel
	 * @return
	 */
	@RequestMapping("getMyCounserLorDesc")
	@ResponseBody
	public CounselorResponse getMyCounserLorDesc(HttpServletRequest request){
		CounselorResponse counselorResponse=new CounselorResponse();
		//TODO判断session
		if(null==request.getSession().getAttribute("counseTel") || "".equals(request.getSession().getAttribute("counseTel"))){
			counselorResponse.setResultCode("-1");
			counselorResponse.setResultDesc("咨询师登录超时");
			return counselorResponse;
		}
		String counseTel=(String)request.getSession().getAttribute("counseTel");
		counselorResponse.setResultCode("0");
		counselorResponse.setResultDesc("success");
		Counselor counselor=counselorService.getCounselor(counseTel);
		String gd="";
		if(counselor.getGoodDirection().contains("1")){
			gd+="学习倦怠,";
		}
		if(counselor.getGoodDirection().contains("2")){
			gd+="亲子关系,";
		}
		if(counselor.getGoodDirection().contains("3")){
			gd+="教育理念,";
		}
		if(counselor.getGoodDirection().contains("4")){
			gd+="学习习惯,";
		}
		if(counselor.getGoodDirection().contains("5")){
			gd+="人际关系,";
		}
		if(counselor.getGoodDirection().contains("6")){
			gd+="沉迷网络,";
		}
		if(counselor.getGoodDirection().contains("7")){
			gd+="情绪管理,";
		}
		if(counselor.getGoodDirection().contains("8")){
			gd+="自控能力,";
		}
		gd=gd.substring(0, gd.length()-1);
		counselor.setGoodDirection(gd);
		counselor.setPersonHead(CommonConfig.getProperty("PIC_URL_FRONT")+counselor.getPersonHead());
		counselorResponse.setCounselor(counselor);
		return counselorResponse;
	}
	
	/**
	 * 咨询师修改资料
	 * @param request
	 * @throws IOException 
	 * @throws ServletException 
	 */
	@RequestMapping("goToUpdateMyData")
	public void myData(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
	      String  counseTel=(String) request.getSession().getAttribute("counseTel");
	      Counselor counselor=counselorService.getCounselor(counseTel);
	      counselor.setPersonHead(CommonConfig.getProperty("PIC_URL_FRONT")+counselor.getPersonHead());
	      String directions=counselor.getGoodDirection();
	      for (int i = 1; i < 9; i++) {
	    	  if(directions.contains(i+"")){
		    	  request.setAttribute("direction"+i,i);
	    	  }
		  }
	      String counseTypes=counselor.getCounseType();
	      for (int i = 1; i <= 4; i++) {
	    	  if(counseTypes.contains(i+"")){
		    	  request.setAttribute("counseType"+i,i);
	    	  }
		  }
	      request.setAttribute("counselor",counselor);
	      request.getRequestDispatcher("business/updateMyCounse.jsp").forward(request, response);
	}
	
	/**
	 * 获取咨询师资料
	 * @param request
	 * @param counseTel
	 * @return
	 */
	@RequestMapping("myData")
	@ResponseBody
	public CounselorResponse myData(HttpServletRequest request,@RequestParam("counseTel") String counseTel){
		CounselorResponse counselorResponse=new CounselorResponse();
		
		//TODO判断session
		
		counselorResponse.setResultCode("0");
		counselorResponse.setResultDesc("success");
		Counselor counselor=counselorService.getCounselor(counseTel);
		String gd="";
		if(counselor.getGoodDirection().contains("1")){
			gd+="学习倦怠,";
		}
		if(counselor.getGoodDirection().contains("2")){
			gd+="亲子关系,";
		}
		if(counselor.getGoodDirection().contains("3")){
			gd+="教育理念,";
		}
		if(counselor.getGoodDirection().contains("4")){
			gd+="学习习惯,";
		}
		if(counselor.getGoodDirection().contains("5")){
			gd+="人际关系,";
		}
		if(counselor.getGoodDirection().contains("6")){
			gd+="沉迷网络,";
		}
		if(counselor.getGoodDirection().contains("7")){
			gd+="情绪管理,";
		}
		if(counselor.getGoodDirection().contains("8")){
			gd+="自控能力,";
		}
		gd=gd.substring(0, gd.length()-1);
		counselor.setGoodDirection(gd);
		counselor.setPersonHead(CommonConfig.getProperty("PIC_URL_FRONT")+counselor.getPersonHead());
		if(counselor.getProfessional().contains("")){
			String temp=counselor.getProfessional();
			counselor.setProfessional(temp.replaceAll("", ""));
		}
		counselorResponse.setCounselor(counselor);
		return counselorResponse;
	}
	
	/**
	 * 获取咨询师资料
	 * @param request
	 * @param counseTel
	 * @return
	 */
	@RequestMapping("myData2")
	@ResponseBody
	public CounselorResponse myData2(HttpServletRequest request,@RequestParam("id") String id){
		CounselorResponse counselorResponse=new CounselorResponse();
		
		//TODO判断session
		
		counselorResponse.setResultCode("0");
		counselorResponse.setResultDesc("success");
		Counselor counselor=counselorService.getCounselor2(id);
		String gd="";
		if(counselor.getGoodDirection().contains("1")){
			gd+="学习倦怠,";
		}
		if(counselor.getGoodDirection().contains("2")){
			gd+="亲子关系,";
		}
		if(counselor.getGoodDirection().contains("3")){
			gd+="教育理念,";
		}
		if(counselor.getGoodDirection().contains("4")){
			gd+="学习习惯,";
		}
		if(counselor.getGoodDirection().contains("5")){
			gd+="人际关系,";
		}
		if(counselor.getGoodDirection().contains("6")){
			gd+="沉迷网络,";
		}
		if(counselor.getGoodDirection().contains("7")){
			gd+="情绪管理,";
		}
		if(counselor.getGoodDirection().contains("8")){
			gd+="自控能力,";
		}
		gd=gd.substring(0, gd.length()-1);
		counselor.setGoodDirection(gd);
		counselor.setPersonHead(CommonConfig.getProperty("PIC_URL_FRONT")+counselor.getPersonHead());
		
		counselorResponse.setCounselor(counselor);
		return counselorResponse;
	}
	
	/**
	 * 获取咨询师资料
	 * @param request
	 * @param counseTel
	 * @return
	 */
	@RequestMapping("myData3")
	@ResponseBody
	public CounselorResponse myData3(HttpServletRequest request,@RequestParam("orderSn") String orderSn){
		CounselorResponse counselorResponse=new CounselorResponse();
		counselorResponse.setResultCode("0");
		counselorResponse.setResultDesc("success");
		Order order=orderService.getOrder(orderSn);
		Counselor counselor=counselorService.getCounselor(order.getCounseTel());
		String gd="";
		if(counselor.getGoodDirection().contains("1")){
			gd+="学习倦怠,";
		}
		if(counselor.getGoodDirection().contains("2")){
			gd+="亲子关系,";
		}
		if(counselor.getGoodDirection().contains("3")){
			gd+="教育理念,";
		}
		if(counselor.getGoodDirection().contains("4")){
			gd+="学习习惯,";
		}
		if(counselor.getGoodDirection().contains("5")){
			gd+="人际关系,";
		}
		if(counselor.getGoodDirection().contains("6")){
			gd+="沉迷网络,";
		}
		if(counselor.getGoodDirection().contains("7")){
			gd+="情绪管理,";
		}
		if(counselor.getGoodDirection().contains("8")){
			gd+="自控能力,";
		}
		gd=gd.substring(0, gd.length()-1);
		counselor.setGoodDirection(gd);
		counselor.setPersonHead(CommonConfig.getProperty("PIC_URL_FRONT")+counselor.getPersonHead());
		
		counselorResponse.setCounselor(counselor);
		return counselorResponse;
	}
	
	/**
	 * 注册号码判重
	 * @param request
	 * @param counseTel
	 * @return
	 */
	@RequestMapping("isRegist")
	@ResponseBody
	public BaseResponse isRegist(HttpServletRequest request,@RequestParam("counseTel") String counseTel){
		BaseResponse baseResponse=new BaseResponse();
		
		if(!counselorService.isRegist(counseTel)){
			baseResponse.setResultCode("-1");
			baseResponse.setResultDesc("counseTel already registered");
			return baseResponse;
		}
		
		baseResponse.setResultCode("0");
		baseResponse.setResultDesc("success");
		return baseResponse;
	}
	
	 /**
	    * @Title: addImage
	    * @Description: 上传图片
	    * @param @param imgFile1
	    * @param @param saveRootPath
	    * @param @param telNumber
	    * @param @return    设定文件
	    * @return String    返回类型
	    * @date 2014-10-27 下午7:14:45
	    * @throws
	    * @author 赵应光
	     */
	    public String addImage(MultipartFile imgFile1,String saveRootPath,String telNumber) { 
	         String path1="personHead";
	         String path2=telNumber;
	       
	         //定义一个数组，用于保存可上传的文件类型 
	         List<String> fileTypes = new ArrayList<String>(); 
	         fileTypes.add("jpg"); 
	         fileTypes.add("jpeg"); 
	         fileTypes.add("bmp"); 
	         fileTypes.add("gif"); 
	         fileTypes.add("png"); 
	          
	         //保存第一张图片 
	         if(!(imgFile1.getOriginalFilename() ==null || "".equals(imgFile1.getOriginalFilename()))) { 
	             File file1 = this.getFile(imgFile1,path1,path2, fileTypes,saveRootPath);
	             return file1.getPath();
	         } 
	          return "";
	        
	    }  
	    
	    /**
	     *
	     * @Description: 保存文件并获取文件完整名
	     * @param @param imgFile
	     * @param @param typeName
	     * @param @param brandName
	     * @param @param fileTypes
	     * @param @param saveRootPath
	     * @param @return  
	     * @return File 
	     * @throws
	     * @author 赵应光
	     * @date 2014-10-27
	     */
	    private File getFile(MultipartFile imgFile,String typeName,String brandName,List<String> fileTypes,String saveRootPath) { 
	        String fileName = imgFile.getOriginalFilename(); 
	        logger.info("fileName="+fileName);
	        //获取上传文件类型的扩展名,先得到.的位置，再截取从.的下一个位置到文件的最后，最后得到扩展名 
	         String ext = fileName.substring(fileName.lastIndexOf(".")+1,fileName.length()); 
	        
	         //对扩展名进行小写转换 
	         ext = ext.toLowerCase(); 
	          
	         File file = null; 
	        
	         if(fileTypes.contains(ext)) {                      //如果扩展名属于允许上传的类型，则创建文件 
	             file = this.creatFolder(typeName, "", brandName+"."+ext,saveRootPath); 
	          
	             try { 
	                imgFile.transferTo(file);                   //保存上传的文件 
	            } catch (IllegalStateException e) { 
	                e.printStackTrace(); 
	            } catch (IOException e) { 
	                e.printStackTrace(); 
	            } 
	         } 
	         return file; 
	    }  
	    
	    /**
	     * @Description: 保存文件到指定路径
	     * @param @param typeName
	     * @param @param brandName
	     * @param @param fileName
	     * @param @param saveRootPath
	     * @param @return  
	     * @return File 
	     * @throws
	     * @author 赵应光
	     * @date 2014-10-27
	     */
	    private File creatFolder(String typeName,String brandName,String fileName,String saveRootPath) { 
	         File file = null; 	         
	         typeName = typeName.replaceAll("/", "");               //去掉"/" 
	         typeName = typeName.replaceAll(" ", "");               //替换半角空格 
	         typeName = typeName.replaceAll(" ", "");               //替换全角空格 
	          
	         brandName = brandName.replaceAll("/", "");             //去掉"/" 
	         brandName = brandName.replaceAll(" ", "");             //替换半角空格 
	         brandName = brandName.replaceAll(" ", "");             //替换全角空格 
	         
	         File firstFolder = new File(saveRootPath + typeName);         //一级文件夹 
	        
	         if(firstFolder.exists()) {                             //如果一级文件夹存在，则检测二级文件夹 
	             File secondFolder = new File(firstFolder,brandName); 
	             if(secondFolder.exists()) {                        //如果二级文件夹也存在，则创建文件 
	                 file = new File(secondFolder,fileName); 
	             }else {                                            //如果二级文件夹不存在，则创建二级文件夹 
	                 secondFolder.mkdir(); 
	                 file = new File(secondFolder,fileName);        //创建完二级文件夹后，再合建文件 
	             } 
	         }else {                                                //如果一级不存在，则创建一级文件夹 
	             firstFolder.mkdir(); 
	             File secondFolder = new File(firstFolder,brandName); 
	             if(secondFolder.exists()) {                        //如果二级文件夹也存在，则创建文件 
	                 file = new File(secondFolder,fileName); 
	             }else {                                            //如果二级文件夹不存在，则创建二级文件夹 
	                 secondFolder.mkdir(); 
	                 file = new File(secondFolder,fileName); 
	             } 
	         } 
	         return file; 
	    }
	    
	    /**
	     * 获取评论
	     * @param request
	     * @return
	     */
	    @RequestMapping("getPinglun")
		@ResponseBody
		public PingLunResponse getPinglun(HttpServletRequest request){
	    	String picNewsId=request.getParameter("picNewsId");
	    	PingLunResponse pingLunResponse=new PingLunResponse();

	    	List<PingLun> pingLunList=counselorService.getPinglun(picNewsId);
	    	if(pingLunList.size()==0){
	    		logger.info("[picNewsId="+picNewsId+"],暂无评论.");
	    		pingLunResponse.setResultCode("-1");
		    	pingLunResponse.setResultDesc("failure");
				return pingLunResponse;
	    	}
	    	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
	    	for (PingLun pingLun : pingLunList) {
	    		pingLun.setAddTimeStr(sdf.format(pingLun.getAddTime()));
			}
	    	pingLunResponse.setResultCode("0");
	    	pingLunResponse.setResultDesc("success");
	    	pingLunResponse.setPingLunList(pingLunList);
			return pingLunResponse;
		}  
	    
	    /**
	     * 咨询师详情页
	     * @param request
	     * @param response
	     * @param tel
	     * @throws IOException
	     * @throws ServletException
	     */
	    @RequestMapping("gotoCounselorDescjsp")
		public void gotoCounselorDescjsp(HttpServletRequest request,HttpServletResponse response,String tel) throws IOException, ServletException{
	    	request.setAttribute("tel",tel);
	    	List<OrderPing> orderList=orderService.getOrderPingjiaList(tel);
	    	SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    	List<FreeAsk> freeAskList=freeAskService.getFreeAskPingjiaList(tel);
	    	for (FreeAsk freeAsk : freeAskList) {
	    		freeAsk.setPingjiaTimeStr(sdf2.format(freeAsk.getCounseAskTime()));
	    		freeAsk.setAskTimeStr(sdf2.format(freeAsk.getAskTime()));
	    		if(freeAsk.getType()==1){
	    			freeAsk.setQuestion(CommonConfig.getProperty("YUYIN_URL_FRONT")+freeAsk.getQuestion());
	    		}
	    		
	    		if(null!=freeAsk.getCounseAnswer() && freeAsk.getCounseAnswer().contains(".mp3")){
	    			freeAsk.setIsYuyin("1");
	    			freeAsk.setCounseAnswer(CommonConfig.getProperty("YUYIN_URL_FRONT")+freeAsk.getCounseAnswer());
	    		}else{
	    			freeAsk.setIsYuyin("0");
	    		}
			}
	    	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
	    	for (OrderPing order : orderList) {
	    		order.setPingjiaTimeStr(sdf.format(order.getPingjiaTime()));
			}
	    	request.setAttribute("orderList",orderList);
	    	request.setAttribute("freeAskList",freeAskList);
	    	request.getRequestDispatcher("counselorDesc.jsp").forward(request, response);
	    }
	    /**
	     * 去评论
	     * @param request
	     */
	    @RequestMapping("goPinglun")
		public void goPinglun(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
	    	String content=request.getParameter("content");
	    	String id=request.getParameter("id");
	    	logger.info("评论号为"+id);
	    	if(null==id || "".equals(id)){
	    		id="null";
	    	}
	    	logger.info("content="+content);
	    	String picNewsId=request.getParameter("picNewsId");
	    	String advertId=request.getParameter("advertId");
	    	request.setAttribute("advertId",advertId);
			WxUser wxUser=(WxUser)request.getSession().getAttribute("wxUser");
			content=ChinaeseUtil.CharTOChinaese(content);
			counselorService.addPingLun(content,picNewsId,wxUser,id);
			
			response.sendRedirect("gotoPicNews?advertId="+advertId);
			
		} 	
	    /**
	     * 登录
	     * @param request
	     */
	    @RequestMapping("gotoMyCounse")
	    @ResponseBody
	    public BaseResponse gotoMyCounse(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
	    	BaseResponse baseResponse=new BaseResponse();
	    	String tel=request.getParameter("tel");
	    	String password=request.getParameter("password");
	    	boolean b=counselorService.checkPassword(tel,password);
	    	if(b){
	    		request.getSession().setAttribute("counseTel",tel);
	    		baseResponse.setResultCode("0");
	    		baseResponse.setResultDesc("success");
	    		return baseResponse;
	    	}else{
	    		baseResponse.setResultCode("-1");
	    		baseResponse.setResultDesc("帐号或密码错误,有疑问，请联系管理员");
	    		return baseResponse;
	    	}
	    } 	
	    
	    @RequestMapping("updatePassword")
	    @ResponseBody
	    public BaseResponse updatePassword(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
	    	BaseResponse baseResponse=new BaseResponse();
	    	String tel=request.getParameter("tel");
	    	String code=request.getParameter("code");
	    	String password=request.getParameter("password");
	    	
	    	if(!authCodeService.isAuthCode(tel, code)){
				logger.info("验证码错误");
				baseResponse.setResultCode("-2");
	    		baseResponse.setResultDesc("验证码错误");
	    		return baseResponse;
			}
	    	
	    	boolean b=counselorService.updatePassword(tel,password);
	    	if(b){
	    		request.getSession().setAttribute("counseTel",tel);
	    		baseResponse.setResultCode("0");
	    		baseResponse.setResultDesc("success");
	    		return baseResponse;
	    	}else{
	    		baseResponse.setResultCode("-1");
	    		baseResponse.setResultDesc("修改密码失败");
	    		return baseResponse;
	    	}
	    } 	
}
